package frontend.user;

import javax.swing.*;
import java.awt.*;

public class SearchPanel extends JPanel {
    private final JTextField searchField;

    public SearchPanel() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        setOpaque(false);

        // 搜索输入框
        searchField = new JTextField(15);
        searchField.setMaximumSize(new Dimension(200, 30));
        searchField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        searchField.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(200, 200, 200)),
                BorderFactory.createEmptyBorder(5, 10, 5, 10)
        ));

        // 搜索按钮
        JButton searchButton = new JButton("搜索");
        searchButton.setFont(new Font("微软雅黑", Font.BOLD, 12));
        searchButton.setBackground(new Color(70, 130, 180));
        searchButton.setForeground(Color.WHITE);
        searchButton.setFocusPainted(false);

        // 组装组件
        add(searchField);
        add(Box.createHorizontalStrut(10));
        add(searchButton);

        // 添加按钮事件监听器接口
        searchButton.addActionListener(e -> {
            String keyword = getKeyword();
            if (keyword.isEmpty()) {
                JOptionPane.showMessageDialog(this, "请输入搜索关键词");
            } else {
                firePropertyChange("search", null, keyword);
            }
        });
    }

    // 获取搜索关键词
    public String getKeyword() {
        return searchField.getText().trim();
    }
}